<template>
  <div class="adduser">
    <!-- 标题 -->
    <h3 class="title">用户注册</h3>
    <!-- 表单（element组件库里面搬来的） -->
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="150px"
      class="demo-ruleForm"
    >
      <!-- 下面是表单的每一项 -->
      <el-form-item label="用户名" prop="userName">
        <el-input v-model="ruleForm.userName"></el-input>
      </el-form-item>
      <el-form-item label="昵称" prop="nikName">
        <el-input v-model="ruleForm.nikName"></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-radio-group v-model="ruleForm.sex">
          <el-radio label="男"></el-radio>
          <el-radio label="女"></el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="年龄" prop="age">
        <el-input v-model="ruleForm.age"></el-input>
      </el-form-item>
      <el-form-item label="联系电话" prop="phone">
        <el-input v-model="ruleForm.phone"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="passWord	">
        <el-input v-model="ruleForm.passWord"></el-input>
      </el-form-item>
      <el-form-item label="职位" prop="post">
        <el-select v-model="ruleForm.post" placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="薪资" prop="money">
        <el-input v-model="ruleForm.money"></el-input>
      </el-form-item>
      <!-- 下面是表单的提交和重置按钮 -->
      <el-form-item>
        <el-button type="primary" :plain="true" @click="submitForm('ruleForm')"
          >添加</el-button
        >
        <el-button type="info" :plain="true" @click="resetForm('ruleForm')"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import adminRequest from "../request/admin";
import cookie from "js-cookie";
export default {
  data() {
    // 下面都是element表单自带的验证信息 （注意：element是一个组件库，相当于把别人写好的功能直接搬过来用）
    var userName = (rule, value, callback) => {
      if (value === "") {
        return callback(new Error("请填写用户名"));
      } else {
        return callback();
      }
    };
    var nikName = (rule, value, callback) => {
      if (value === "") {
        return callback(new Error("请填写用户昵称"));
      } else {
        return callback();
      }
    };
    var sex = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("请选择用户性别"));
      } else {
        return callback();
      }
    };
    var age = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("请填写用户年龄"));
      } else {
        return callback();
      }
    };
    var phone = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("请填写用户电话"));
      } else {
        return callback();
      }
    };
    var passWord = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("请设置用户密码"));
      } else {
        return callback();
      }
    };
    var post = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("请填写用户职位"));
      } else {
        return callback();
      }
    };
    var money = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("请填写用户薪资"));
      } else {
        return callback();
      }
    };
    return {
      // 下面是element表单对应的字段 后面需要与接口参数一一对应
      ruleForm: {
        userName: "", //用户名称
        nikName: "", //用户名称
        sex: "", //性别
        age: "", //年龄
        phone: "", //电话
        passWord: "", //密码
        post: "", //职位
        money: "", //薪资
      },
      // 下面是element表单自带的验证规则
      rules: {
        userName: [{ validator: userName, trigger: "blur" }],
        nikName: [{ validator: nikName, trigger: "blur" }],
        sex: [{ validator: sex, trigger: "blur" }],
        age: [{ validator: age, trigger: "blur" }],
        phone: [{ validator: phone, trigger: "blur" }],
        passWord: [{ validator: passWord, trigger: "blur" }],
        post: [{ validator: post, trigger: "blur" }],
        money: [{ validator: money, trigger: "blur" }],
      },
      // options为下拉框的数据 提供选择（相当于是定死的五条数据，让用户只能选这五种）
      options: [
        {
          value: "经理",
          label: "经理",
        },
        {
          value: "员工",
          label: "员工",
        },
      ],
    };
  },

  methods: {
    // 添加客户办理记录
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        //下面是如果表单信息完整 才能执行的操作（element自带的判断）
        if (valid) {
          // 请求添加客户的接口
          adminRequest
            .addUser({
              // 下面是接口需要传入的参数   与element表单各个字段一一对应
              userName: this.ruleForm.userName,
              nikName: this.ruleForm.nikName,
              sex: this.ruleForm.sex,
              age: this.ruleForm.age,
              phone: this.ruleForm.phone,
              passWord: this.ruleForm.passWord,
              post: this.ruleForm.post,
              money: this.ruleForm.money,
            })
            .then((res) => {
              // 如果请求成功执行的操作
              if (res.data.success == true) {
                this.$message.success("注册成功!");
                this.resetForm();
                // 请求失败显示失败的提示语
              } else {
                  if(res.data.code == 401){
                     window.localStorage.removeItem("userinfo");
                      cookie.remove("satoken");
                      this.$router.push({ path: "/" });
                      this.$notify({
                        title: "需要登录才能访问哦",
                        type: "warning"
                      });
                  }
                this.$message.error(res.data);
              }
            });
          //下面是如果表单信息不完整 才能执行的操作（element自带的判断）
        } else {
        
          this.$message.error("请检查信息是否完整！");
          return false;
        }
      });
    },
    // 清空表单数据（element自带的判断）
    resetForm() {
      this.$refs.ruleForm.resetFields();
    },
  },

  created() {},
};
</script>

<style lang="scss" scoped>
// 下面是页面结构的css样式 （这里样式比较少就不用放在另外的文件引入了）
.adduser {
  overflow: hidden;

  ::v-deep .title {
    text-align: center;
    font-size: 20px;
  }
  ::v-deep .demo-ruleForm {
    width: 500px;
    margin: 30px auto;
  }
  ::v-deep .el-input__inner {
    height: 45px;
    font-size: 16px;
  }
  ::v-deep .el-form-item__label {
    font-size: 16px;
    position: relative;
    top: 10px;
  }
  ::v-deep .el-form-item__content {
    margin-top: 10px;
    height: 50px;
    display: flex;
  }
  ::v-deep .el-form-item__error {
    font-size: 16px;
  }

  ::v-deep .el-select-dropdown__item {
    display: flex;
    justify-content: center;
  }
  ::v-deep .el-radio-group {
    width: 100px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-top: 11px;
  }
}
</style>